package com.lejin.order.controller;

import com.lejin.order.entity.OrderInfo;
import com.lejin.order.entity.OrderPay;
import com.lejin.order.service.WxpayService;
import com.lejin.order.service.impl.CourseOrderPayServiceImpl;
import com.lejin.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/order")
public class CourseOrderPayController {
    @Autowired
    private CourseOrderPayServiceImpl courseOrderPayService;
    @Autowired
    private WxpayService wxpayService;
    @PostMapping("/pay")
    public ResultVO addOrderPay(@RequestBody OrderInfo orderInfo){
        String orderId = courseOrderPayService.saveOrderPay(orderInfo);
        if (orderId==null){
            return new ResultVO(false,"保存订单失败",null);
        }else{
            String payUrl=wxpayService.requestPayUrl(orderId,orderInfo.getCourseName());
            if (payUrl==null){
                return new ResultVO(false,"保存订单失败",null);
            }else{
                HashMap<String,Object> map= new HashMap<>();
                map.put("payUrl",payUrl);
                map.put("orderId",orderId);
                map.put("orderName",orderInfo.getCourseName());
                map.put("orderPrice",orderInfo.getPricePaid());
                return new ResultVO(true,"保存订单成功",map);
            }

        }
    }

    //检查订单状态，并修改为2.
    @GetMapping("check")
    public ResultVO checkState(String orderId){
        OrderPay order = courseOrderPayService.getById(orderId);
        if (!order.getOrderStatus().equals(2)){
            order.setOrderStatus(2);

        }
        return new ResultVO(true,"出售成功",order.getOrderStatus());
    }
}
